///////////////////////////////////////////////////////////////////////////////
//
// lighthouse3D - 3D Software Library
//
// File: C2DScreen.h
//
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2003-2013 Marcus Reinhardt
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
///////////////////////////////////////////////////////////////////////////////
//
// You can find a copy of the GNU General Public License (gpl.txt) in the
// directory of the library.
//
///////////////////////////////////////////////////////////////////////////////

#ifndef _C2DScreen_H_
#define _C2DScreen_H_

#include "CBasicClasses.h"
#include "CColorDefs.h"
#include "CFilter.h"
#include "CFileHandler.h"
#include "CFont.h"
#ifdef __MULTI_CORE_SUPPORT
#include "lh3dmthread.h"
#endif

///////////////////////////////////////////////////////////////////////////////
// class T2DScreen : Storage for an 2D screen
///////////////////////////////////////////////////////////////////////////////

//! Storage for an 2D screen

class C2DScreen
{

public:

    C2DScreen();
    ~C2DScreen();

    unsigned int getPixel(int x, int y);
    unsigned int getPixelRelative(int x, int y, int relX, int relY);
    
    int getSizeX() {return sizeX;}
    int getSizeY() {return sizeY;}

    // Basic paint operations
    void clear();
    void drawLine(int x1, int y1, int x2, int y2, unsigned int c);
    void drawSolidBox(int x1, int y1, int x2, int y2, unsigned int c);
    void drawBox(int x1, int y1, int x2, int y2, int y3, unsigned int c1, unsigned int c2, unsigned int c3);
    void drawGUIBox(int x1, int y1, int x2, int y2, int y3, unsigned int c1, unsigned int c2, unsigned int c3, char *t);
    void drawGUIStdBox(int x1, int y1, int x2, int y2, int y3, char *t);
    void drawPicture(int x1, int y1, TPictureData *p);
    bool drawText(int x, int y, char *t);
    bool drawTextCentered(int x1, int y1, int x2, int y2, char *t);
    bool drawLetter(int x, int y, int l);
    int  getTextLength(char *t);
        
	bool loadFont(char *fontName);

    // Basic pixel operations
    void setPixel(int x, int y, unsigned int c);
    void setPixelFast(int x, int y, unsigned int c);
    void setPixelTransparent(int x, int y, unsigned int c);
    void setGUITransparency(float t) {guiTransparency = t; };
    void setSize(int sX, int sY);
    void setFontColor(unsigned int c);

    // Extended screen operations
    void bilinearScrFilter();
    
    bool setBackgroundPicture(char *f);
    bool setBackgroundColor(unsigned int c);

    bool saveBufferAsTGA(char *f);

    T2DData *getScreenHandle();
    TRGBColorTriple getRGBColor(unsigned int c);

    // GUI elements
    TGUIHandle *getGUIHandle();
    TGUIHandle *getMainHandle() { return MainHandle; };
    TGUIHandle *createButton(TGUIHandle *parent, int x, int y, int w, int h, char *caption);
    void repaintButton(int h);
    TGUIHandle *createWindow(TGUIHandle *parent, int x,int y, int w, int h, int h2, char *caption);
    void repaintWindow(int h);
    TGUIHandle *createLabel(TGUIHandle *parent, int x, int y, int color, char *caption);
    void repaintLabel(int h);
    void setCaption(TGUIHandle *h, char *c) {strcpy(h->caption, c); };
	void setVisible(TGUIHandle *h, bool v) {h->visible = v; };
    bool isOverGUIElement(TGUIHandle *h, int x, int y);
    bool isOverNoGUIElement(int x, int y);
    void repaintGUIElements();
    
private:

    // For the internal painting routines (e.g. drawLine)
    void setPixelUnchecked(int x, int y, unsigned int c);

    int sizeX, sizeY;
    float guiTransparency;
    unsigned int bcolor, fontColor;
    bool useBColor, fontReady;
    int lastAssignedHandle;

    CFilter f;
    T2DData d;
    CFileHandler fileHandler;
    TPictureData backPicData;
    TPictureData backPicDataResized;
	TPictureData p;
    CFont font;

    TGUIHandle managedGUIHandles[_GUIHandles_];
	TGUIHandle *MainHandle;

	int cachedY, cachedSizeX, cachedYPos;
};

#endif
